package com.tqz.juc.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * <p>
 *
 *
 * @author tianqingzhao
 * @since 2022/1/24 19:56
 */
@Slf4j
public class LockSupportTest {

    public static void main(String[] args) {

        Thread t0 = new Thread(new Runnable() {

            @Override
            public void run() {
                Thread current = Thread.currentThread();
                log.info("{},开始执行!",current.getName());
                for(;;){//spin 自旋
                    log.info("准备park住当前线程：{}....",current.getName());
                    // 无参和有参的park方法区别：无参的只会阻塞一次，一旦被下面的线程中断以后，再次进来就不会阻塞了。
                    // 而有参的每次都会阻塞。
                    LockSupport.park();
                    log.info("当前线程{}已经被唤醒....",current.getName());
                }
            }

        },"t0");

        t0.start();

        try {
            Thread.sleep(5000);
            log.info("准备唤醒{}线程!",t0.getName());
            LockSupport.unpark(t0);
    
//            Thread.sleep(2000);
//            t0.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
